<template>
  <div>
    <FilterTable
      ref="table"
      columnsRef="jzyRepairPro"
      :height="200"
      :columns="columns"
      :onCellChange="changeCells"
      :dataSource="list"
      :onColumnsChange="columns => (this.columns = columns)"
      :onSyncTableData="syncTableFun"
    >
      <template slot="moreActions">
        <span @click="delRecordHandle">批量删除</span>
      </template>
      <template slot="controls">
        <el-button size="small" type="primary" icon="el-icon-plus" :disabled="lab" @click="showAddNew = true">新增</el-button>
      </template>
    </FilterTable>
    <BaseDialog :visible.sync="showAddNew" title="附加项目选择" width="400px" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <AddForm ref="addForm" @close="closeDialog" />
    </BaseDialog>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import AddForm from './addForm';
import { getSfqfList, getSflbList } from '@/api/repair/business/customer/additional';

export default {
  name: 'additionalPro',
  components: {
    AddForm
  },
  mixins: [dictionary],
  props: ['dataList', 'empty', 'lab'],
  data() {
    return {
      columns: this.createTableColumns(),
      list: [],
      showAddNew: false
    };
  },
  watch: {
    dataList(val) {
      this.insertRecords(val);
    },
    empty(val) {
      if (val) {
        this.changeempty();
      }
    }
  },
  mounted() {
    this.getSfqfList();
    this.getSflbList();
    this.createBaseTable();
    this.insertRecords(this.dataList);
    this.changeempty();
  },
  methods: {
    createTableColumns() {
      return [
        {
          title: '操作',
          minWidth: 60,
          render: props => {
            return (
              <el-button type="text" size="medium" onClick={() => this.edit(props.row)}>
                编辑
              </el-button>
            );
          }
        },
        {
          title: '序号',
          dataIndex: 'index',
          width: 50,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '收费类别',
          dataIndex: 'MANAGE_SORT_CODE',
          minWidth: 100,
          sorter: true,
          showOverflowTooltip: true,
          editable: true,
          defaultEditable: true,
          editType: 'select',
          editItems: []
        },
        {
          title: '收费区分',
          dataIndex: 'CHARGE_PARTITION_CODE',
          minWidth: 100,
          sorter: true,
          showOverflowTooltip: true,
          editable: true,
          defaultEditable: true,
          editType: 'select',
          editItems: []
        },
        {
          title: '附加项目代码',
          dataIndex: 'ADD_ITEM_CODE',
          minWidth: 120,
          sorter: true,
          showOverflowTooltip: true
        },
        {
          title: '附加项目名称',
          dataIndex: 'ADD_ITEM_NAME',
          minWidth: 120,
          sorter: true,
          showOverflowTooltip: true
        },
        {
          title: '附加项目费',
          dataIndex: 'ADD_ITEM_PRICE',
          minWidth: 110,
          sorter: true,
          showOverflowTooltip: true
        },
        {
          title: '优惠金额',
          dataIndex: 'DISCOUNT_AMOUNT',
          minWidth: 100,
          sorter: true,
          showOverflowTooltip: true,
          numberFormat: true
        },
        {
          title: '折扣率',
          dataIndex: 'DISCOUNT',
          minWidth: 90,
          sorter: true,
          showOverflowTooltip: true,
          editable: true,
          defaultEditable: true,
          editType: 'input'
        },
        {
          title: '实收金额',
          dataIndex: 'ADD_ITEM_AMOUNT',
          minWidth: 100,
          sorter: true,
          showOverflowTooltip: true,
          numberFormat: true
        },
        {
          title: '备注',
          dataIndex: 'REMARK',
          minWidth: 170,
          sorter: true,
          showOverflowTooltip: true,
          editable: true,
          defaultEditable: true,
          editType: 'input'
        },
        {
          title: '活动编号',
          dataIndex: 'ACTIVITY_CODE',
          minWidth: 100,
          sorter: true,
          showOverflowTooltip: true
        }
      ];
    },
    async getSfqfList() {
      const res = await getSfqfList();
      // console.log(res)
      this.columns[3].editItems = res.map(x => ({ value: x.CHARGE_PARTITION_CODE, text: x.CHARGE_PARTITION_NAME }));
    },
    async getSflbList() {
      const res = await getSflbList();
      // console.log(res)
      this.columns[2].editItems = res.map(x => ({ value: x.MANAGE_SORT_CODE, text: x.MANAGE_SORT_NAME }));
    },
    delRecordHandle() {
      this.BaseTable.EXECUTE_DELETE();
    },
    addRecordHandle() {
      this.visible_drawer = true;
    },
    edit(data) {
      this.showAddNew = true;
      this.$nextTick(() => {
        // console.log(this.$refs.addForm)
        this.$refs.addForm.openModel(data);
      });
    },
    createBaseTable() {
      this.BaseTable = this.$refs.table.$refs.pageTable;
    },
    insertRecords(list) {
      this.BaseTable.EXECUTE_INSERT(list);
    },
    syncTableFun(val) {
      this.$emit('syncTablelist', val);
    },
    // 清空列表
    changeempty() {
      if (this.empty) {
        this.list = [];
        this.$emit('changeEmpty', false);
      }
      return;
    },
    closeDialog(val, data) {
      this.showAddNew = val;
      // console.log(val)
      if (data) {
        // console.log(data)
        if (data.index == null) {
          var flag = true;
          for (const item of this.list) {
            if (item.ADD_ITEM_NAME == data.list.ADD_ITEM_NAME) {
              flag = false;
            }
          }
          if (flag) {
            data.list.DISCOUNT = 1;
            data.list.ADD_ITEM_AMOUNT = data.list.ADD_ITEM_PRICE * data.list.DISCOUNT;
            data.list.DISCOUNT_AMOUNT = data.list.ADD_ITEM_PRICE - data.list.ADD_ITEM_AMOUNT;
            this.insertRecords(data.list);
            this.$notify.success({ title: '提示信息', message: '新增成功!' });
          } else {
            this.$notify.info({ title: '提示信息', message: '新增失败!项目已存在' });
          }
        } else {
          this.list[data.index].ADD_ITEM_NAME = data.list.ADD_ITEM_NAME;
          this.list[data.index].ADD_ITEM_CODE = data.list.ADD_ITEM_CODE;
          this.list[data.index].ADD_ITEM_PRICE = data.list.ADD_ITEM_PRICE;
          // console.log('money',this.list[data.index])
          this.list[data.index].ADD_ITEM_AMOUNT = this.list[data.index].ADD_ITEM_PRICE * this.list[data.index].DISCOUNT;
          this.list[data.index].DISCOUNT_AMOUNT = this.list[data.index].ADD_ITEM_PRICE - this.list[data.index].ADD_ITEM_AMOUNT;
          this.list[data.index].CHARGE_PARTITION_CODE = data.list.CHARGE_PARTITION_CODE;
          this.list[data.index].REMARK = data.list.REMARK;
          this.$notify.success({ title: '提示信息', message: '修改成功!' });
        }
      }
    },
    changeCells(keys, obj) {
      let key = Object.keys(keys)[0];
      let a = key.substring(key.indexOf('|') + 1);
      if (a == 'DISCOUNT') {
        obj.ADD_ITEM_AMOUNT = obj.ADD_ITEM_PRICE * obj.DISCOUNT; // parseFloat
        obj.DISCOUNT_AMOUNT = obj.ADD_ITEM_PRICE - obj.ADD_ITEM_AMOUNT;
      }
      // console.log('changeCells',a,obj)
    }
  }
};
</script>

<style lang="less"></style>
